/*
 * Copyright (c) 2022. All rights reserved.
 * Please don't try to modify the code, because I don't know what the consequences will be.
 *
 *
 *
 *
 *
 *
 *
 */

package cc.zzone.util;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.util.StringUtils;

import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * SpringBoot 启动工具
 *
 * @author jinzhengze
 * @since 2022-07-01
 */
@Slf4j
public final class StartUtil {

    private StartUtil() {
    }

    public static void run(Class<?> primarySource, String... args) {
        ConfigurableApplicationContext ctx = new SpringApplicationBuilder(primarySource)
                .properties("spring.main.allow-bean-definition-overriding=true")
                .run(args);
        String port = ctx.getEnvironment().getProperty("server.port");
        String path = ctx.getEnvironment().getProperty("server.servlet.context-path");
        port = StringUtils.hasText(port) ? port : "8080";
        path = StringUtils.hasText(path) ? path : "/";
        System.out.printf("""
                       _                  _
                  ___ | |_   ____   ____ | |_
                 /___)|  _) / _  | / ___)|  _)
                |___ || |__( ( | || |    | |__
                (___/  \\___)\\_||_||_|     \\___)    by zzone
                                
                App running at:
                - Local:    http://127.0.0.1:%s%s
                - Network:  http://%s:%s%s
                """, port, path, getHostIp(), port, path);

        Runtime runtime = Runtime.getRuntime();
        try {
            runtime.exec("rundll32 url.dll,FileProtocolHandler " +
                    String.format("http://127.0.0.1:%s%s", port, path));
        } catch (IOException ignored) {
        }
    }

    private static String getHostIp() {
        String ip = "127.0.0.1";
        try {
            Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
            while (netInterfaces.hasMoreElements()) {
                NetworkInterface ni = netInterfaces.nextElement();
                for (Enumeration<InetAddress> inetAddresses = ni.getInetAddresses();
                     inetAddresses.hasMoreElements(); ) {
                    InetAddress ia = inetAddresses.nextElement();
                    if (ia.isLoopbackAddress())
                        continue;
                    if (!ia.getHostAddress().contains("::") &&
                            !ia.getHostAddress().contains("0:0:") &&
                            !ia.getHostAddress().contains("fe80")) {
                        ip = ia.getHostAddress();
                        break;
                    }
                }
            }
        } catch (Exception ignored) {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException ignored1) {
            }
        }
        return ip;
    }
}
